package com.jiang.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.lang.StringUtils;


public class MyFilter implements Filter {

	private String encoding = "UTF-8"; //默认的编码格式
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		String _encoding = filterConfig.getInitParameter("encoding");
		if(StringUtils.isNotBlank(_encoding)){
			this.encoding = _encoding;
		}
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
			// 设置接收与返回的编码
		request.setCharacterEncoding(this.encoding);
		response.setCharacterEncoding(this.encoding);
		chain.doFilter(request, response);// 放行
	}

	@Override
	public void destroy() {
		
	}

}
